package com.yunshipei.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.yunshipei.base.BaseActivity;
import com.yunshipei.model.UserInfo;
import com.yunshipei.enterplorer.R;
import com.yunshipei.ui.view.CollectionLongPressDialog;
import com.yunshipei.ui.view.TouchImageView;
import com.yunshipei.utils.ToastUtils;

import java.io.File;

public class ContactsAvatarActivity extends BaseActivity {
    TouchImageView avatar;
    UserInfo userInfo;
    String imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_avatar);
        getWindow().setBackgroundDrawable(null);
        avatar = (TouchImageView) findViewById(R.id.avatar);
        Intent intent = getIntent();
        userInfo = (UserInfo) intent.getSerializableExtra("userinfo");
        imgUrl = userInfo.getAvatar();

        Glide.with(this).load(imgUrl).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) avatar.getDrawable();
                if(bitmapDrawable!=null)
                {
                    if(bitmapDrawable.getBitmap().isRecycled())
                        bitmapDrawable.getBitmap().recycle();
                }
                avatar.setImageBitmap(resource);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                avatar.setVisibility(View.GONE);
                Toast.makeText(ContactsAvatarActivity.this, "图片加载失败", Toast.LENGTH_SHORT).show();
            }
        });
        initViewListener();
    }

    @Override
    protected void onDestroy() {
        if(avatar.getDrawable()!=null)
        {
            if(((BitmapDrawable) avatar.getDrawable()).getBitmap().isRecycled())
                 ((BitmapDrawable)avatar.getDrawable()).getBitmap().recycle();
        }
        super.onDestroy();
    }

    private void initViewListener() {
        avatar.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Drawable drawable = avatar.getDrawable();
                if (drawable != null) {
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                    final  Bitmap bitmap = bitmapDrawable.getBitmap();
                    if (bitmap == null) {
                        ToastUtils.showToast("下载头像失败");
                    } else {
                        final CollectionLongPressDialog dialog = new CollectionLongPressDialog(ContactsAvatarActivity.this, R.style.MyDialogStyle);
                        dialog.setCanceledOnTouchOutside(true);
                        View customView = dialog.getCustomView();
                        TextView save = (TextView) customView.findViewById(R.id.del_collection);
                        save.setText("保存头像到本地");
                        save.setBackgroundColor(getResources().getColor(R.color.white));
                        save.setTextColor(getResources().getColor(R.color.black));
                        TextView title = (TextView) customView.findViewById(R.id.collection_title);
                        title.setVisibility(View.GONE);
                        save.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                File rootFile;
                                if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                                    rootFile = new File("/sdcard/DCIM/camera");
                                } else {
                                    rootFile = new File("/system/DCIM/camera");
                                }
                                File imgFile = new File(rootFile, imgUrl.substring(imgUrl.lastIndexOf("/")));
                                MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imgUrl.substring(imgUrl.lastIndexOf("/")),null);
                                ContactsAvatarActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(imgFile.getAbsolutePath()))));
                                ToastUtils.showToast("保存头像成功");
                            }

                        });
                        dialog.show();
                    }
                }
                return true;
            }
        });
    }
}



